package com.the4man.framework.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.LogInfoService;
import com.the4man.framework.model.LogInfo;
import com.the4man.framework.model.User;
import com.the4man.framework.pagemodel.PUser;
import com.the4man.framework.service.UserInfoServiceI;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.utils.InitParamUtil;
import com.the4man.framework.utils.RequestUtil;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

/**
 * Created by lenovo on 2017/12/12.
 */
@Action(
        value = "userAction",
        results = {@Result(
                name = "success",
                location = "/index.jsp"
        ), @Result(
                name = "check",
                location = "/check.jsp"
        ), @Result(
                name = "error",
                location = "/login.jsp"
        )}
)
public class UserAction extends BaseAction implements ModelDriven<PUser> {
    private static final Logger logger = LoggerFactory.getLogger(UserAction.class);
    private static final long serialVersionUID = -1244022624336881132L;
    @Autowired
    private LogInfoService logService;
    @Autowired
    UserInfoServiceI userInfoService;
    private PUser puser = new PUser();
    public static String cookieValue;
    public static String userData;

    public UserAction() {
    }

    public static String getUserData() {
        return userData;
    }

    public static void setUserData(String userData) {
        userData = userData;
    }

    public static String getCookieValue() {
        return cookieValue;
    }

    public static void setCookieValue(String cookieValue) {
        cookieValue = cookieValue;
    }

    public String login() {
        String forward = "error";

        try {
            User user = new User();
            String pi = this.puser.getUserid();
            String pt = this.puser.getLoginType();
            user.setUserid(this.puser.getUserid());
            user.setLoginType(this.puser.getLoginType() == null?"DQM":this.puser.getLoginType());
            user = this.userInfoService.getUser(user);
            if(user != null) {
                BeanUtils.copyProperties(user, this.puser);
                this.puser.setLogserialnumber(DateUtil.getInstance().getTimeStamp());
                RequestUtil.putHttpSessionObject(InitParamUtil.getSessionInfoName(), this.puser);
                RequestUtil.putOgnlObject("user", user);
                LogInfo logInfo = new LogInfo();
                logInfo.setId(UUID.randomUUID().toString());
                logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                logInfo.setFunc("登入");
                logInfo.setOp("登入");
                logInfo.setMemo(user.getUsername() + "登入系统");
                this.logService.insert(logInfo);
                forward = "success";
            }
        } catch (Exception var6) {
            logger.error("用户登录出现异常", var6);
        }

        return forward;
    }

    public void clientlogin() {
        try {
            User user = new User();
            user.setUserid(this.puser.getUserid());
            user.setLoginType(this.puser.getLoginType() == null?"DQM":this.puser.getLoginType());
            user = this.userInfoService.getUser(user);
            PageJson json = new PageJson();
            if(user != null) {
                BeanUtils.copyProperties(user, this.puser);
                this.puser.setLogserialnumber(DateUtil.getInstance().getTimeStamp());
                RequestUtil.putHttpSessionObject(InitParamUtil.getSessionInfoName(), this.puser);
                RequestUtil.putOgnlObject("user", user);
                json.setReturnMsg("用户登陆成功！");
                json.setReturnFlag(true);
                this.writeJson(json);
            } else {
                json.setReturnMsg("用户登陆失败！");
                json.setReturnFlag(false);
                this.writeJson(json);
            }
        } catch (Exception var3) {
            logger.error("用户登录出现异常", var3);
        }

    }

    public void validateDqmUser() {
        PageJson pageJson = new PageJson();

        try {
            this.puser.setLoginType("DQM");
            this.userInfoService.validateDqmUser(pageJson, this.puser);
            this.writeJson(pageJson);
        } catch (Exception var3) {
            logger.error("校验DQM用户失败", var3);
            pageJson.setReturnFlag(false);
            pageJson.setReturnMsg("系统内部错误");
            this.writeJson(pageJson);
        }

    }

    public String eazySign() {
        String forward = "error";
        Cookie[] cookies = this.getRequest().getCookies();
        if(cookies != null) {
            String key = "";
            String value = "";
            Cookie[] var8 = cookies;
            int var7 = cookies.length;

            for(int var6 = 0; var6 < var7; ++var6) {
                Cookie cookie = var8[var6];
                if(cookie.getName().equals("LIVEE_SESSION_ID")) {
                    key = cookie.getName();
                    value = cookie.getValue();
                    break;
                }
            }

            if(key != null && !key.equals("")) {
                setCookieValue(value);
                this.getRequest().getSession().setAttribute("livee", value);
                forward = "check";
            }
        }

        return forward;
    }

    public void UserData() {
        String userD = this.getRequest().getParameter("loginData");
        setUserData(userD);
        this.writeJson("");
    }

    public String checkLogin() {
        String forward = "error";
        String data = getUserData();
        Cookie[] cookies = this.getRequest().getCookies();
        if(cookies != null) {
            String key = "";
            String value = "";
            Cookie[] var9 = cookies;
            int var8 = cookies.length;

            for(int var7 = 0; var7 < var8; ++var7) {
                Cookie cookie = var9[var7];
                if(cookie.getName().equals("LIVEE_SESSION_ID")) {
                    key = cookie.getName();
                    value = cookie.getValue();
                    break;
                }
            }

            if(value.equals(getCookieValue()) && cookieValue != "" && value != "" && value != null) {
                JSONObject jsStr = JSONObject.fromObject(data);
                String userName = jsStr.getString("User_Account");
                String loginState = jsStr.getString("Login");
                if(loginState.equals("true")) {
                    try {
                        User user = new User();
                        user.setUserid(userName);
                        user.setLoginType("DQM");
                        user = this.userInfoService.getUser(user);
                        if(user != null) {
                            BeanUtils.copyProperties(user, this.puser);
                            this.puser.setLogserialnumber(DateUtil.getInstance().getTimeStamp());
                            RequestUtil.putHttpSessionObject(InitParamUtil.getSessionInfoName(), this.puser);
                            RequestUtil.putOgnlObject("user", user);
                            forward = "success";
                        }
                    } catch (Exception var10) {
                        logger.error("用户登录出现异常", var10);
                    }
                }
            }
        }

        return forward;
    }

    public String logout() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        if(session != null) {
            session.invalidate();
        }

        return "error";
    }

    public void clientlogout() {
        HttpSession session = ServletActionContext.getRequest().getSession();
        if(session != null) {
            session.invalidate();
            PageJson json = new PageJson();
            json.setReturnMsg("用户登出成功！");
            json.setReturnFlag(true);
            this.writeJson(json);
        }

    }

    public PUser getModel() {
        return this.puser;
    }
}
